package com.utc.fjc.command.revise;

import java.awt.Frame;
import java.awt.HeadlessException;
import java.util.Arrays;

import javax.swing.JOptionPane;

import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.commands.revise.ReviseDialog;
import com.teamcenter.rac.commands.revise.RevisePanel;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
import com.utc.common.util.FJCIdUtils;
import com.utc.teamcenter.common.util.ItemUtil;
import com.utc.teamcenter.common.util.QueryUtil;

public class FJCERPDesignRevisePanel extends RevisePanel {

	private static final long serialVersionUID = 1L;
	public static String ObjectType = "object_type"; //$NON-NLS-1$
	public static String ERPDesignRevision = "Ng4_DesignRevision"; //$NON-NLS-1$
	public static String NONERPDesignRevision = "Ng4_Non_ERP_DsgnRevision"; //$NON-NLS-1$
	public static String ERPPartRevision = "Ng4_PartRevision"; //$NON-NLS-1$
	private static String ITEM_REVISION_ID = "item_revision_id"; //$NON-NLS-1$
	private static String ITEM_ID = "item_id"; //$NON-NLS-1$

	public FJCERPDesignRevisePanel(Frame frame, TCSession tcsession,
			ReviseDialog revisedialog, Boolean boolean1) {
		super(frame, tcsession, revisedialog, boolean1);
	}

	public FJCERPDesignRevisePanel(Frame frame, TCSession tcsession,
			ReviseDialog revisedialog) {
		super(frame, tcsession, revisedialog);
	}

	public void loadPanels() {
		super.loadPanels();

		InterfaceAIFComponent[] targets = AIFUtility.getCurrentApplication()
				.getTargetComponents();
		TCComponent tcSelectedComp = null;

		try {
			if (targets != null && targets.length > 0) {
				tcSelectedComp = (TCComponent) targets[0];

				if (tcSelectedComp != null
						&& tcSelectedComp instanceof TCComponentItemRevision) {
					String compType = tcSelectedComp.getType();
					System.out.println("compType " + compType);
					if (ERPDesignRevision.equals(compType)
							|| ERPPartRevision.equals(compType)
							|| NONERPDesignRevision.equals(compType)) {

						Object[] possibleValues = {
								"Major Revision", "Minor Revision" }; //$NON-NLS-1$ //$NON-NLS-2$
						String selectedValue = (String) JOptionPane
								.showInputDialog(
										null,
										"Select the Part Revise type", "Revise Type", //$NON-NLS-1$ //$NON-NLS-2$
										1, null, possibleValues,
										possibleValues[0]);

						if (selectedValue.equalsIgnoreCase("Major Revision")) { //$NON-NLS-1$
							System.out.println("Major Revision"); //$NON-NLS-1$
							TCComponentItemRevision rev = ItemUtil
									.getMaxItemRevision((TCComponentItemRevision) tcSelectedComp);
							String item_revision_id = rev
									.getStringProperty(ITEM_REVISION_ID);

							if (ERPDesignRevision.equals(compType)
									|| NONERPDesignRevision.equals(compType)) {
								if (item_revision_id.length() < 2) {
									MessageBox.post(Messages.error1,
											Messages.infomation,
											MessageBox.INFORMATION);
									this.itemInfoPanel.revTextField.setText(""); //$NON-NLS-1$
								} else {
									String outRevId = FJCIdUtils
											.getERPDesignMajorRevId(item_revision_id);
									this.itemInfoPanel.revTextField
											.setText(outRevId);
								}
							}

							if (ERPPartRevision.equals(compType)) {
								if (item_revision_id.length() < 3) {
									MessageBox.post(Messages.error1,
											Messages.infomation,
											MessageBox.INFORMATION);
									this.itemInfoPanel.revTextField.setText(""); //$NON-NLS-1$
								} else {
									String outRevId = FJCIdUtils
											.getERPPartMajorRevId(item_revision_id);
									this.itemInfoPanel.revTextField
											.setText(outRevId);
								}
							}

						}

						if (selectedValue.equalsIgnoreCase("Minor Revision")) { //$NON-NLS-1$
							System.out.println("Minor Revision"); //$NON-NLS-1$
							TCComponentItemRevision rev = (TCComponentItemRevision) tcSelectedComp;
							String item_id = rev.getStringProperty(ITEM_ID);
							String item_revision_id = rev
									.getStringProperty(ITEM_REVISION_ID);

							if (ERPDesignRevision.equals(compType)
									|| NONERPDesignRevision.equals(compType)) {
								if (item_revision_id.length() < 2) {
									MessageBox.post(Messages.error2,
											Messages.infomation,
											MessageBox.INFORMATION);
									this.itemInfoPanel.revTextField.setText(""); //$NON-NLS-1$
								}
								String query_item_revision_id = item_revision_id
										.substring(0, 2)
										+ ".*"; //$NON-NLS-1$
								String queryType = Messages.FJCERPDesignRevisePanel_QUERY_ITEMREVISION;
								String[] entries = new String[] {
										Messages.FJCERPDesignRevisePanel_QUERY_ITEM_ID,
										Messages.FJCERPDesignRevisePanel_QUERY_ITEM_REVISION_ID }; //$NON-NLS-1$ //$NON-NLS-1$ //$NON-NLS-1$
								String[] values = new String[] { item_id,
										query_item_revision_id };
								TCComponent[] result = QueryUtil.query(
										queryType, entries, values);

								String outRevId = FJCIdUtils
										.getERPDesignMinorRevId(
												item_revision_id
														.substring(0, 2),
												result);
								this.itemInfoPanel.revTextField
										.setText(outRevId);
							}

							if (ERPPartRevision.equals(compType)) {
								if (item_revision_id.length() < 3) {
									MessageBox.post(Messages.error2,
											Messages.infomation,
											MessageBox.INFORMATION);
									this.itemInfoPanel.revTextField.setText(""); //$NON-NLS-1$
								}
								String query_item_revision_id = item_revision_id
										.substring(0, 3)
										+ ".*"; //$NON-NLS-1$
								String queryType = Messages.FJCERPDesignRevisePanel_QUERY_ITEMREVISION;
								String[] entries = new String[] {
										Messages.FJCERPDesignRevisePanel_TYPE,
										Messages.FJCERPDesignRevisePanel_QUERY_ITEM_ID,
										Messages.FJCERPDesignRevisePanel_QUERY_ITEM_REVISION_ID }; //$NON-NLS-1$ //$NON-NLS-1$ //$NON-NLS-1$
								String[] values = new String[] {
										Messages.FJCERPDesignRevisePanel_ERP_Part_Revision,
										item_id, query_item_revision_id };
								TCComponent[] result = QueryUtil.query(
										queryType, entries, values);

								String outRevId = FJCIdUtils
										.getERPDesignMinorRevId(
												item_revision_id
														.substring(0, 3),
												result);
								this.itemInfoPanel.revTextField
										.setText(outRevId);
							}

						}
					}

				}
			}
		} catch (HeadlessException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

}
